package com.tarena.lbs.content.controller;

import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.model.Result;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.content.pojo.param.ArticleCategoryParam;
import com.tarena.lbs.content.pojo.query.ArticleCategoryQuery;
import com.tarena.lbs.content.pojo.vo.ArticleCategoryVO;
import com.tarena.lbs.content.service.CategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 处理文章分类 业务模块功能
 */
@Tag(name = "文章分类管理")
@RestController
@RequestMapping("/admin/content/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 新增文章分类
     */
    @Operation(summary = "新增文章分类")
    @PostMapping(value = "/insert", consumes = "application/x-www-form-urlencoded") // 明确支持表单格式
    public Result<Void> addCategory(
            @Validated ArticleCategoryParam param // 去掉@RequestBody，直接接收表单参数
    ) throws BusinessException {
        categoryService.addCategory(param);
        return Result.success();
    }

    /**
     * 文章分类分页列表查询
     */
    @Operation(summary = "文章分类列表查询")
    @GetMapping("/list")
    public Result<PageResult<ArticleCategoryVO>> pageList(ArticleCategoryQuery query) throws BusinessException {
        PageResult<ArticleCategoryVO> pageResult = categoryService.pageList(query);
        // 使用带数据的构造函数，手动指定成功状态
        return new Result<>(pageResult);
    }

    /**
     * 修改分类状态（启用/禁用）
     */
    @Operation(summary = "修改分类状态")
    @PostMapping("/update")
    public Result<Void> updateStatus(
            @Parameter(description = "分类ID", required = true) @RequestParam Integer id,
            @Parameter(description = "目标状态（1=启用，2=禁用）", required = true) @RequestParam Integer categoryStatus
    ) throws BusinessException {
        categoryService.updateStatus(id, categoryStatus);
        return Result.success();
    }

    @PostMapping("/delete")
    public Result<Void> deleteCategory(@RequestBody ArticleCategoryParam param) throws BusinessException {
        categoryService.deleteCategory(param.getId()); // 从param中获取id
        return Result.success();
    }
}